'use strict';

const Controller = require('egg').Controller;
var jwt = require('jsonwebtoken');
class UserController extends Controller {
  async login() {
    const { ctx } = this;
   let {pwd,username}=ctx.request.body
  let res= await ctx.service.user.login(pwd,username)
  if(res.length)
  {
 let token= jwt.sign({...res[0]}, 'secret', { expiresIn: '1h' });
      ctx.body={
          code:1,
          msg:"登陆成功",
          token:token,
          uid:res[0].id
      }
  }
  else{
    ctx.body={
        code:0,
        msg:"登陆失败"
    }
  }
  }
}

module.exports = UserController;